package com.qls.mt.biz.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.qls.core.ali.order.bookrq.request.BookRq;
import com.qls.core.ali.order.cancelrq.CancelRq;
import com.qls.core.ali.order.validaterq.request.ValidateRq;
import com.qls.core.utils.R;
import com.qls.mt.biz.common.utils.MtResponse;
import com.qls.mt.biz.order.service.MtOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Administrator
 */
@Slf4j
@RestController
public class OrderController {


    @Autowired
    private MtOrderService mtOrderService;

    @PostMapping(value = "order/validate")
    public R validate(@RequestBody ValidateRq validateRq) {
        return mtOrderService.validate(validateRq);
    }

    @PostMapping(value = "order/book")
    public R book(@RequestBody BookRq bookRq) {
        return mtOrderService.book(bookRq);
    }

    @PostMapping(value = "order/cancel")
    public R cancel(@RequestBody CancelRq cancelRq) {
        return mtOrderService.cancel(cancelRq);
    }


    @PostMapping(value = "callback/order")
    public MtResponse callback(@RequestBody JSONObject jsonObject) {
        log.info("订单通知：{}", jsonObject);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        try {
            mtOrderService.callback(dataObject);
        } catch (Exception e) {
            e.printStackTrace();
            return MtResponse.error();
        }
        return MtResponse.ok();
    }


}
